package com.hrt.manage.member.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.HRTMember;
import com.hrt.manage.member.service.HRTMemberService;
import com.hrt.util.GetAgeUtil;
import com.hrt.util.MD5Tool;

@Controller
@RequestMapping(value = "/manage/member")
public class HRTMemberController extends BaseController<HRTMember> {
	@Autowired
	private HRTMemberService hrtMemberService;

	public HRTMemberController() {

	}

	public HRTMemberService getHrtMemberService() {
		return hrtMemberService;
	}

	public void setHrtMemberService(HRTMemberService hrtMemberService) {
		this.hrtMemberService = hrtMemberService;
	}

	public List<HRTMember> getList() {
		return list;
	}

	public void setList(List<HRTMember> list) {
		this.list = list;
	}

	/**
	 * 
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(HttpServletRequest request) {
		ModelAndView view = new ModelAndView();
		System.out.println("list");
		list = hrtMemberService.queryByPage(getPageIndex(), getPageSize());
		setTotalCount(hrtMemberService.totalCount());
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		view.addObject("pageResult", page);
		// request.getSession().setAttribute("pageResult", page);
		return view;

	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Long[] delete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletResponse response) throws IOException {
		Long[] idArr = null;
		ModelAndView view = new ModelAndView();

		if (ids != null) {
			if (ids.indexOf(",") != -1) {
				String[] idStrs = ids.split(",");
				idArr = new Long[idStrs.length];
				for (int idx = 0; idx < idStrs.length; idx++) {
					idArr[idx] = Long.parseLong(idStrs[idx]);
				}
			} else {
				idArr = new Long[] { Long.parseLong(ids) };
			}

		}
		try {
			hrtMemberService.deleteById(idArr);

			return idArr;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

	@RequestMapping(value = "/save.do", method = RequestMethod.POST)
	public void save(@ModelAttribute("memberInfo") HRTMember member,
			HttpServletRequest request, HttpServletResponse response)
	{
		System.out.println("save");
		String password = "111111";
		String borth = request.getParameter("born");
		String pasd = MD5Tool.MD5(password);
		Date registTime = new Date();
		// 根据生日计算年龄
		int age = GetAgeUtil.getAge(borth, GetAgeUtil.getStringDate());
		member.setAge(age);
		// 默认设置密码位“111111”
		member.setPassword(pasd);
		// 设置添加时间
		member.setRegisterTime(registTime);
		// 默认设置激活
		member.setIsMailVaild(1);
		
		try {
			hrtMemberService.saveMember(member);

			response.sendRedirect("list.do");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@RequestMapping(value = "/show")
	@ResponseBody
	public Map show(
			@RequestParam(value = "id", required = true) String memberId,
			HttpServletRequest request, HttpServletResponse response) {

		Map map = new HashMap();
		if (memberId != null && !"".equals(memberId)) {
			// Map resultMap=new HashMap<K, V>();
			System.out.println("showshowshowshowshow " + memberId);
			HRTMember member = this.hrtMemberService.getMemberByid(new Long(
					memberId));
			if (member != null) {
				map = transBean2Map(member);
			}

		}
		return map;

	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public void update(@ModelAttribute("memberInfo") HRTMember member,
			HttpServletRequest request, HttpServletResponse response) {
		String mbrName = member.getMbrName();
		String mail = member.getMail();
		String mobile = member.getMobile();
		String note = member.getNote();
		Integer sex = member.getSex();
		System.out.println("sex:::"+sex);
		Integer statu = member.getStatu();
		String borth = request.getParameter("born");
		// 根据生日计算年龄
		int age = GetAgeUtil.getAge(borth, GetAgeUtil.getStringDate());
		member = this.hrtMemberService.getMemberByid(new Long(member.getId()));
		System.out.println("update");
		member.setAge(age);
		member.setMbrName(mbrName);
		member.setMail(mail);
		member.setNote(note);
		member.setMobile(mobile);
		member.setSex(sex);
		member.setBorn(member.getBorn());
		member.setStatu(statu);
		hrtMemberService.updateMember(member);

		try {
			response.sendRedirect("list.do");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 
	 * @param pageIndex
	 * 
	 * @param pageSize
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryByPage")
	@ResponseBody
	public Map queryByPage(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			HttpServletRequest request) {
		Map resultMap = new HashMap();

		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);

		setPageSize(pageSize);

		setTotalCount(hrtMemberService.totalCount());

		List<HRTMember> resultList = hrtMemberService.queryByPage(
				getPageIndex(), getPageSize());
		setList(resultList);

		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		resultMap.put("pageResult", page);
		return resultMap;
	}
	
}





